package cn.cxl.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 *
 *======================================================
 *
 * 作者：YXW
 *
 * 创建时间：2018/11/8 16:05
 *
 * 描述：轻量级存储
 *
 * 修订历史：
 *
 * ======================================================
 *
 */

public class SharedPreferencesUtils
{
    private final static String name = "CXL";
    
    private final static int mode = Context.MODE_PRIVATE;
    
    /**
     * 保存首选项
     *
     * @param context
     * @param key
     * @param value
     */
    public synchronized static void saveBoolean(Context context, String key, boolean value)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }
    
    public synchronized static void saveInt(Context context, String key, int value)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.commit();
    }
    
    public synchronized static void saveString(Context context, String key, String value)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }
    
    /**
     * 移除指定键的数据
     *
     * @param context
     * @param key
     */
    public static void removeString(Context context, String key)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        SharedPreferences.Editor edit = sp.edit();
        edit.remove(key);
        edit.commit();
    }
    
    /**
     * 获取首选项
     *
     * @param context
     * @param key
     * @return
     */
    public static boolean getBoolean(Context context, String key)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getBoolean(key, false);
    }
    
    public static boolean getBoolean(Context context, String key, boolean defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getBoolean(key, defValue);
    }
    
    public static boolean getBoolean1(Context context, String key)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getBoolean(key, true);
    }
    
    public static int getInt(Context context, String key)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getInt(key, 0);
    }
    
    public static int getInt(Context context, String key, int defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getInt(key, defValue);
    }
    
    public static String getString(Context context, String key)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getString(key, "");
    }
    
    public static String getString(Context context, String key, String defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getString(key, defValue);
    }
}
